package org.xiaov.extra.mail.strategy.impl;

import org.springframework.mail.MailSendException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.xiaov.bean.mail.Mail;
import org.xiaov.core.string.StringHelper;
import org.xiaov.extra.mail.strategy.MailStrategy;

import javax.annotation.Resource;

/**
 * <p>
 * 纯文本邮件
 * </p>
 *
 * @author xiaovcloud
 * @since 2021/2/26 12:36
 */
@Service
public class TextMail implements MailStrategy {
    @Resource
    private JavaMailSender javaMailSender;

    @Override
    public void sendMail(Mail mail) {
        SimpleMailMessage message = new SimpleMailMessage();
        try {
            message.setFrom(mail.getFrom());
            message.setTo(mail.getTo());
            message.setSubject(mail.getTitle());
            message.setText(mail.getContent());
            javaMailSender.send(message);
            log.info("纯文本的邮件已经发送给{}。", StringHelper.printArray(mail.getTo()));
        } catch (Exception e) {
            log.error("纯文本邮件发送时发生异常！", e);
            throw new MailSendException("纯文本邮件发送时发生异常！", e);
        }
    }
}
